<?php
require_once './function.php';

$json = do_post_request("http://localhost:9095/getnewlisten");
$list = json_decode($json, true);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Cache-Control" content="no-transform">
    <title>W-M-Learning</title>
    <link rel="shortcut icon" href="/Public/img/favicon.ico"/>
    <link href="./Public/css/bootstrap.css" rel="stylesheet">
    <link href="./Public/css/main.css" rel="stylesheet" type="text/css" charset="utf-8"/>
    <link href="./Public/css/index.css" rel="stylesheet" type="text/css" charset="utf-8"/>
    <link href="./Public/css/detail.css" rel="stylesheet" type="text/css" charset="utf-8"/>
    <link href="./Public/css/user.css" rel="stylesheet" type="text/css" charset="utf-8"/>
    <link href="./Public/css/announcement.css" rel="stylesheet" type="text/css" charset="utf-8"/>
    <link href="./Public/css/bootstrap-datetimepicker.min.css" rel="stylesheet" type="text/css" charset="utf-8"/>
    <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript" src="Public/js/jquery.cookie.js"></script>
    <script>
        var rootUrl = '';
    </script>
</head>
<body>

<div class="home">

    <header class="header">
    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <div class="row">
                <div class="hidden-xs hidden-sm col-md-3 col-lg-3">
                    <div class="navbar-header navbar-left">
                        <a class="navbar-brand-mc" href="/">
                            <img src="./Public/img/logo.png" alt="logo"/>
                        </a>
                    </div>
                </div>
                <div class="hidden-xs visible-sm-block col-sm-2">
                    <div class="navbar-header navbar-left">
                        <a class="navbar-brand-mc" href="/">
                            <img src="/Public/img/Brand-M.png" alt="logo"/>
                        </a>
                    </div>
                </div>
                <div class="visible-xs-block col-xs-2">
                    <div class="navbar-header navbar-left">
                        <a class="navbar-brand-mc" href="/">
                            <img src="/Public/img/Brand-S.png" alt="logo"/>
                        </a>
                    </div>
                </div>
                <div class="col-xs-8 col-sm-5 col-md-5 col-lg-5">
                     <!--<div class="navbar-search navbar-left">-->
                        <form class="navbar-form-mc" method="get" action="/course/search" role="search">
                            <div class="input-group input-group-nav">
                                <input type="text" class="form-control" id="input-search" name="key" placeholder="查找试卷...">
                                <span class="input-group-btn">
                                    <a role="button" href="search.html" style="background-image:url('/Public/img/icon-search.png');height:100%;" class="btn" aria-label="Left Align" onclick="$(this).closest('form').submit()">
                                    </a>
                                </span>
                            </div>
                        </form>
                    <!--</div>-->
                </div>
                <div id="list1">
                <div class="hidden-xs col-sm-5 col-md-4 col-lg-4">
                    <div id="navbar" class="navbar-collapse collapse">
                        <ul class="nav navbar-nav pull-right">
                            <li id="nav-index"><a href="/">首页</a></li>
                            <li id="nav-course"><a href="course.php">试卷</a></li>
                        
                            <li><a href="/">登录</a></li>
                                
                        </ul>
                    </div>
                </div>
                <div class="visible-xs-block col-xs-2">
                    <div class="nav nav-hamburger pull-right">
                        <a href="#ham" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-menu-hamburger" aria-hidden="true"></span></a>
                        <ul class="dropdown-menu pull-right">
                            <li><a href="/">首页</a></li>
                            <li><a href="/course">试卷</a></li>
                         
                            <li><a href="/">登录</a></li>
                                
                        </ul>
                    </div>

                </div>
                </div>
        <div id="list2" style="display: none">
           <div class="hidden-xs col-sm-5 col-md-4 col-lg-4">
                    <div id="navbar" class="navbar-collapse collapse">
                        <ul class="nav navbar-nav pull-right">
                            <li id="nav-index"><a href="/">首页</a></li>
                            <li id="nav-course"><a href="/course">课程</a></li>
                            <li id="nav-notice"><a href="/announcement">公告</a></li>
                                                            <li id="nav-user" class="dropdown">
                                    <a href="#user" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">个人 <span class="caret"></span></a>
                                    <ul class="dropdown-menu pull-right">
                                        <li><a href="userhome.html">个人主页</a></li>
                                        <li><a href="/">资料设置</a></li>
                                        <li><a href="/user/logout" class="logout">退出登录</a></li>
                                    </ul>
                                </li>
                        </ul>
                    </div>
                </div>
                <div class="visible-xs-block col-xs-2">
                    <div class="nav nav-hamburger pull-right">
                        <a href="#ham" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-menu-hamburger" aria-hidden="true"></span></a>
                        <ul class="dropdown-menu pull-right">
                            <li><a href="/">首页</a></li>
                            <li><a href="/course">课程</a></li>
                            <li id="xs-nav-notice"><a href="/announcement">公告</a></li>
                                                            <li><a href="/user">个人主页</a></li>
                                <li><a href="/user/basic">资料设置</a></li>
                                <li><a href="/user/logout" class="logout">退出登录</a></li>
                        </ul>
                    </div>
                </div>
                </div>
            </div>
        </div>
    </nav>
</header>

    <div class="content">

        <div class="wrapper">
            <div class="container">
                <div class="row">
                    <div class="wrapper-intro col-xs-0 col-sm-6 col-md-8 col-lg-8">
                        <img src="./Public/img/logo.png"/>
                        <br/>
                        
                    </div>

                    <!--<div class="wrapper-intro col-xs-0 col-sm-0 col-md-2 col-lg-2"></div>-->

                    <div class="log-or-sign col-xs-12 col-sm-6 col-md-4 col-lg-4">
                        <!-- Nav tabs -->
                        <ul class="nav nav-tabs" id="tab-sl">
                            <li role="presentation" class="active"><a href="#login" aria-controls="login" role="tab" data-toggle="tab" id="tri-login">登录</a></li>
                            <li role="presentation"><a href="#register" aria-controls="register" role="tab" data-toggle="tab" id="tri-register">注册·加入</a></li>
                        </ul>

                        <!-- Tab panes -->
                        <div class="tab-content">
                            <!-- Tab Log in -->
                            <form role="tabpanel" class="tab-pane fade in active" id="login" onsubmit="return false;">
                                <div class="tab-info" id="login-info"></div>
                                <label for="login-email" class="sr-only">邮箱</label>
                                <input type="email" id="login-email" class="form-control" placeholder="邮箱" required autofocus>
                                <label for="login-password" class="sr-only">密码</label>
                                <input type="password" id="login-password" class="form-control" placeholder="密码" required>
                                <div class="checkbox">
                                <label class="f-white">
                                <input type="checkbox" id="login-remember" checked> 记住我
                                </label>
                                <a class="pull-right forgot" href="/recover.html">忘记密码</a>
                                </div>
                                <button class="btn btn-lg btn-primary btn-block login" onclick="login_onclick()">登录</button>
                            </form>
                            <!-- Tab Register -->
                            <form role="tabpanel" class="tab-pane fade" id="register" onsubmit="return false;">
                                <div class="tab-info" id="register-info"></div>
                                <label for="register-email" class="sr-only">邮箱</label>
                                <input type="email" id="register-email" class="form-control" placeholder="邮箱" required autofocus>
                                <label for="register-password" class="sr-only">密码</label>
                                <input type="password" id="register-password" class="form-control" placeholder="密码" required>
                                <button class="btn btn-lg btn-primary btn-block register" onclick="register_onclick()">注册</button>
                            </form>
                        </div>
                    </div>                </div>
            </div>
        </div>
      <div class="home-page container">
            <div class="row">
                <div class="col-xs-12 col-sm-12 col-md-8 col-lg-8">
                    <!--BEGIN NEW COURSES-->
                    <div class="section section-carousel">
                        <h4>最新试卷</h4>

                        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <?php
                            foreach($list as $item){
                                echo '<ul>
                                    <li><a href="exercise.php?id='.$item['id'].'">'.$item['listenName'].'</a></li>
                            </ul>
                            ';                                
                            }                            
                            ?>
                        </div>
                    </div>
                    
                
                         <div class="section section-carousel">
                        <h4>最新试卷</h4>

                        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <?php
                            foreach($list as $item){
                                echo '<ul>
                                    <li><a href="exercise.php?id='.$item['id'].'">'.$item['listenName'].'</a></li>
                            </ul>
                            ';                                
                            }                            
                            ?>
                        </div>
                    </div>
                    
                    <div class="section hot-course">
                        <h4>最热试卷<small><a href="course.html">更多»</a></small></h4>
                        <?php
                            foreach($list as $item){
                                echo '<ul>
                                    <li><a href="exercise.php?id='.$item['id'].'">'.$item['listenName'].'</a></li>
                            </ul>
                            ';                                
                            }                            
                            ?>

                    </div>
                    <!--END HOT-COURSES-->


                </div>
                <div class="home-sub col-xs-12 col-sm-12 col-md-4 col-lg-4">
                    <div class="hot-tag section">
                        <h4>标签推荐</h4>
                    <a class="tag tag-info" href="CET4.php">CET4</a>
                    <a class="tag tag-info" href="CET6.php">CET6</a>
                    <a class="tag tag-info" href="professional.php">专业英语</a>
                    <a class="tag tag-info" href="IELTS.php">雅思</a>
                    <a class="tag tag-info" href="TOEFL.php">托福</a>
                </div>

                    </div>
                </div>
            </div>
        </div>


<!-- Modal Feedback End -->
<script src="/Public/js/footer.js" ></script>
<script src="/Public/js/header.js" ></script>
<script src="//cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="./Public/js/jquery.toaster.js"></script>
<script src="./Public/js/index.js"></script>
<script>
    $("#nav-index").addClass('active');
</script>
<script>
      function onload(){
      var list1=document.getElementById("list1");
      var list2=document.getElementById("list2");
      $.get(
             "http://localhost:9095/getuserstatus",
     {
         
     },
     function(data)
     {
         if(data){
//             alert("yes");
             $('.log-or-sign').hide();
             list1.style.display="none";
            list2.style.display="block";
         }
         else{
//             alert("no");
         }
     }
                );
  }
  window.reload=onload();
  function login_onclick(){
		var userEmail=document.getElementById("login-email").value;
		var userPwd=document.getElementById("password").value;
                var checkEmail= /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
                if (!checkEmail.test(userEmail)) {
                    var text = "邮箱格式错误，请重新输入";
                    $('#login-info').show();
                    $('#login-info').html(text);
                    $('#login-email').focus();
                    return;
                }
		$.post(
			"http://localhost:9095/login",
			{userEmail:userEmail,userPwd:userPwd},
			function(data){
				if(data){
					$('.log-or-sign').hide();
                    $.cookie("userEmail",userEmail,{expires:7});
                    $.cookie("userPwd",userPwd,{expires:7});
				}
                                else{
                                    alert("用户名或密码错误");
                                }
			},"text"
		);
}

    function register_onclick(){
		var userName=document.getElementById("register-username").value;
		var userPwd=document.getElementById("register-password").value;
                var checkEmail= /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
                if (!checkEmail.test(userName)) {
                    var text = "邮箱格式错误，请重新输入";
                    $('#register-info').show();
                    $('#register-info').html(text);
                    $('#register-email').focus();
                    return;
                }
                if (userPwd.length < 6) {
                    var text = "密码需要至少为6位";
                    $('#register-info').show();
                    $('#register-info').html(text);
                    $('#register-password').focus();
                    return;
                }
		$.post(
			"http://localhost:9095/newuser",
			{
                            userName:userName,
                            userPwd:userPwd,
                            userEmail:userName
                        },
			function(data){
                            alert("注册成功");
                            $('#tab-sl a[href="#login"]').tab('show');			
			}
            else{
            alert("注册失败")
            },"text"
		)
}
</script>
</body>
</html>